package com.zhongge.gui;

import com.zhongge.book.Book;
import com.zhongge.book.BookList;
import com.zhongge.operations.IOperation;
import com.zhongge.user.AdminUser;
import com.zhongge.user.NormalUser;
import com.zhongge.user.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 主界面窗口
 */
public class MainFrame extends JFrame {
    private User currentUser;
    private BookList bookList;
    private JTable bookTable;
    private DefaultTableModel tableModel;
    private JLabel statusLabel;
    
    public MainFrame() {
        // 获取当前用户和图书列表
        currentUser = GUIMain.getCurrentUser();
        bookList = GUIMain.getBookList();
        
        // 设置窗口基本属性
        setTitle("图书管理系统 - " + currentUser.getName());
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        
        // 创建菜单栏
        JMenuBar menuBar = createMenuBar();
        setJMenuBar(menuBar);
        
        // 创建图书列表区域
        JPanel bookPanel = createBookPanel();
        mainPanel.add(bookPanel, BorderLayout.CENTER);
        
        // 创建状态栏
        statusLabel = new JLabel("就绪");
        statusLabel.setBorder(BorderFactory.createEtchedBorder());
        mainPanel.add(statusLabel, BorderLayout.SOUTH);
        
        // 设置内容面板
        setContentPane(mainPanel);
        
        // 刷新图书列表
        refreshBookList();
    }
    
    /**
     * 创建菜单栏
     */
    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        
        // 文件菜单
        JMenu fileMenu = new JMenu("文件");
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(e -> System.exit(0));
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);
        
        // 操作菜单 - 根据用户类型动态生成
        JMenu operationMenu = new JMenu("操作");
        
        if (currentUser instanceof AdminUser) {
            // 管理员菜单
            addMenuItem(operationMenu, "查找图书", e -> showSearchDialog());
            addMenuItem(operationMenu, "新增图书", e -> showAddBookDialog());
            addMenuItem(operationMenu, "删除图书", e -> showDeleteBookDialog());
            addMenuItem(operationMenu, "显示图书", e -> refreshBookList());
        } else if (currentUser instanceof NormalUser) {
            // 普通用户菜单
            addMenuItem(operationMenu, "查找图书", e -> showSearchDialog());
            addMenuItem(operationMenu, "借阅图书", e -> showBorrowBookDialog());
            addMenuItem(operationMenu, "归还图书", e -> showReturnBookDialog());
        }
        
        menuBar.add(operationMenu);
        
        // 帮助菜单
        JMenu helpMenu = new JMenu("帮助");
        JMenuItem aboutItem = new JMenuItem("关于");
        aboutItem.addActionListener(e -> JOptionPane.showMessageDialog(this, 
                "图书管理系统 v1.0\n使用Java Swing开发", "关于", JOptionPane.INFORMATION_MESSAGE));
        helpMenu.add(aboutItem);
        menuBar.add(helpMenu);
        
        return menuBar;
    }
    
    /**
     * 添加菜单项
     */
    private void addMenuItem(JMenu menu, String text, ActionListener listener) {
        JMenuItem item = new JMenuItem(text);
        item.addActionListener(listener);
        menu.add(item);
    }
    
    /**
     * 创建图书列表面板
     */
    private JPanel createBookPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        
        // 创建表格模型
        String[] columnNames = {"书名", "作者", "价格", "类型", "状态"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 表格不可编辑
            }
        };
        
        // 创建表格
        bookTable = new JTable(tableModel);
        bookTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(bookTable);
        panel.add(scrollPane, BorderLayout.CENTER);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton refreshButton = new JButton("刷新列表");
        refreshButton.addActionListener(e -> refreshBookList());
        buttonPanel.add(refreshButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        
        return panel;
    }
    
    /**
     * 刷新图书列表
     */
    public void refreshBookList() {
        // 清空表格
        tableModel.setRowCount(0);
        
        // 添加图书数据
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            Object[] rowData = {
                book.getName(),
                book.getAuthor(),
                book.getPrice(),
                book.getType(),
                book.isBorrow() ? "已借出" : "未借出"
            };
            tableModel.addRow(rowData);
        }
        
        updateStatus("共显示 " + bookList.getUsedSize() + " 本图书");
    }
    
    /**
     * 更新状态栏信息
     */
    public void updateStatus(String message) {
        statusLabel.setText(message);
    }
    
    /**
     * 显示查找图书对话框
     */
    private void showSearchDialog() {
        SearchBookDialog dialog = new SearchBookDialog(this, bookList);
        dialog.setVisible(true);
        if (dialog.isSearchCompleted()) {
            refreshBookList();
        }
    }
    
    /**
     * 显示新增图书对话框
     */
    private void showAddBookDialog() {
        AddBookDialog dialog = new AddBookDialog(this, bookList);
        dialog.setVisible(true);
        if (dialog.isAddCompleted()) {
            refreshBookList();
            updateStatus("新增图书成功");
        }
    }
    
    /**
     * 显示删除图书对话框
     */
    private void showDeleteBookDialog() {
        DeleteBookDialog dialog = new DeleteBookDialog(this, bookList);
        dialog.setVisible(true);
        if (dialog.isDeleteCompleted()) {
            refreshBookList();
            updateStatus("删除图书成功");
        }
    }
    
    /**
     * 显示借阅图书对话框
     */
    private void showBorrowBookDialog() {
        BorrowBookDialog dialog = new BorrowBookDialog(this, bookList);
        dialog.setVisible(true);
        if (dialog.isBorrowCompleted()) {
            refreshBookList();
            updateStatus("借阅图书成功");
        }
    }
    
    /**
     * 显示归还图书对话框
     */
    private void showReturnBookDialog() {
        ReturnBookDialog dialog = new ReturnBookDialog(this, bookList);
        dialog.setVisible(true);
        if (dialog.isReturnCompleted()) {
            refreshBookList();
            updateStatus("归还图书成功");
        }
    }
}